import tkinter as tk
from tkinter import ttk, messagebox
import ipaddress

def calculate_ipv4():
    try:
        ip = ipaddress.ip_interface(f"{ipv4_entry.get()}/{ipv4_mask_entry.get()}")
        network_address = ip.network.network_address
        hosts = list(ip.network.hosts())
        
        if hosts:
            first_host = hosts[0]
            last_host = hosts[-1]
        else:
            first_host = "No hosts for /32"
        
        result_var.set(f"Network: {network_address}\nHost: {first_host} to {last_host}")
        root.clipboard_clear()
        root.clipboard_append(f"Network: {network_address}\nHost: {first_host} to {last_host}")
        messagebox.showinfo("Info", "IPv4 addresses copied to clipboard!")
    except ValueError:
        messagebox.showerror("Error", "Invalid IPv4 Address or Mask!")

def calculate_ipv6():
    try:
        ip = ipaddress.ip_interface(f"{ipv6_entry.get()}/{ipv6_mask_entry.get()}")
        network_address = ip.network.network_address
        hosts = list(ip.network.hosts())
        
        if hosts:
            first_host = hosts[0]
            last_host = hosts[-1]
        else:
            first_host = "No hosts for /128"
        
        result_var6.set(f"Network: {network_address}\nHost: {first_host} to {last_host}")
        root.clipboard_clear()
        root.clipboard_append(f"Network: {network_address}\nHost: {first_host} to {last_host}")
        messagebox.showinfo("Info", "IPv6 addresses copied to clipboard!")
    except ValueError:
        messagebox.showerror("Error", "Invalid IPv6 Address or Mask!")

def close_app():
    root.destroy()

root = tk.Tk()
root.title("IP Address Tool")

frame = ttk.Frame(root, padding="10")
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))

# IPv4 Entries
ttk.Label(frame, text="IPv4 Address:").grid(column=0, row=0, sticky=tk.W, pady=5)
ipv4_entry = ttk.Entry(frame)
ipv4_entry.grid(column=1, row=0, pady=5)

ttk.Label(frame, text="IPv4 Mask:").grid(column=0, row=1, sticky=tk.W, pady=5)
ipv4_mask_entry = ttk.Entry(frame)
ipv4_mask_entry.grid(column=1, row=1, pady=5)

ipv4_btn = ttk.Button(frame, text="Calculate IPv4", command=calculate_ipv4)
ipv4_btn.grid(column=1, row=2, pady=10)

result_var = tk.StringVar()
ttk.Label(frame, textvariable=result_var).grid(column=1, row=3, pady=5)

# IPv6 Entries
ttk.Label(frame, text="IPv6 Address:").grid(column=0, row=4, sticky=tk.W, pady=5)
ipv6_entry = ttk.Entry(frame)
ipv6_entry.grid(column=1, row=4, pady=5)

ttk.Label(frame, text="IPv6 Mask:").grid(column=0, row=5, sticky=tk.W, pady=5)
ipv6_mask_entry = ttk.Entry(frame)
ipv6_mask_entry.grid(column=1, row=5, pady=5)

ipv6_btn = ttk.Button(frame, text="Calculate IPv6", command=calculate_ipv6)
ipv6_btn.grid(column=1, row=6, pady=10)

result_var6 = tk.StringVar()
ttk.Label(frame, textvariable=result_var6).grid(column=1, row=7, pady=5)

close_btn = ttk.Button(frame, text="Close", command=close_app)
close_btn.grid(column=1, row=8, pady=5)

root.mainloop()